package view.controller;

import common.Message;
import common.NowUser;
import common.User;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import server.ClientConnServer;
import utils.MD5;
import view.Personal;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;
import java.util.concurrent.BrokenBarrierException;

public class ChangeInformationCotroller implements Initializable {
    public TextField name;
    public TextField email;
    public TextField address;
    public TextArea sign;
    public ComboBox gender;
    public DatePicker birth;
    public Button savebt;
    public ImageView image;
    public File localFile;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        NowUser common = new NowUser();
        User user=new User();
        user=common.getUser();
        //System.out.println(user.getUsername());
        if(user.getName()!=null){
            name.setText(user.getName());
        }
        if(user.getEmail()!= null){
            email.setText(user.getEmail());
        }
        if(user.getGender()!=null && !user.getGender().equals("")){
            gender.setValue(user.getGender());
        }
        if(user.getAddress()!=null && !user.getAddress().equals("")){
            address.setText(user.getAddress());
        }
        if(user.getBirth()!=null && !user.getBirth().equals("") && !user.getBirth().equals("null")){
            DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate date=LocalDate.parse(user.getBirth(),dateTimeFormatter);
            birth.setValue(date);
        }
        if(user.getSign()!=null  && !user.getSign().equals("")){
            sign.setText(user.getSign());
        }
        if(user.getHeadimage()!=null && !user.getHeadimage().equals("") && !user.getHeadimage().equals("null")){
            System.out.println(user.getHeadimage());
            Image image1 = new Image("file:"+user.getHeadimage());
            image.setImage(image1);
        }else{
            Image image1=new Image("view\\photo\\默认头像.jpg");
            image.setImage(image1);
        }
        gender.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                System.out.println(newValue);
            }
        });
    }

    //上传头像
    public void toupimage(ActionEvent actionEvent) throws IOException {
        FileChooser fileChooser = new FileChooser(); //文件选择窗口
        fileChooser.setTitle("Open Resource File");  //窗口名
        fileChooser.setInitialDirectory(new File("C:\\Users")); //设置默认打开的文件路径
        File file = fileChooser.showOpenDialog(new Stage());//显示屏幕选择窗口
        // String filePath = file.getCanonicalPath()+"/fileSource"; //指定路径
        String str =null;
        if(file != null) {
            str = String.valueOf(file.getAbsolutePath());  // 方法返回选择文件的相对路径
            String url = "file:" + str;
            NowUser.user.setHeadimage(url);
            image.setImage(new Image(url,127,108,true,true,true));
        }
        //读取本地文件
        localFile = new File(str);
    }

    public void changesave(MouseEvent mouseEvent) throws IOException, BrokenBarrierException, InterruptedException {
        User user = new User();
        MD5 md5 = new MD5();
        if(name.getText().equals("")){
            JOptionPane.showMessageDialog(null, "昵称不能为空", "提示", JOptionPane.ERROR_MESSAGE);
        }else {
            if(name!=null){
                user.setName(name.getText());}
            if(gender.getValue()!=null){
                user.setGender(gender.getSelectionModel().getSelectedItem().toString());}
            System.out.println(user.getGender());
            if(address!=null){
                user.setAddress(address.getText()); }
            if(birth.getValue()!=null){
                String str=birth.getValue().toString();
                user.setBirth(str);}
            user.setSign(sign.getText());
            user.setHeadimage(NowUser.user.getHeadimage());
            user.setId(NowUser.user.getId());
            Message cgmsg = new Message();
            cgmsg.setUser(user);
            //System.out.println("sss"+cgmsg.getUser().getGender());
            cgmsg.setHeadimage(localFile);
            cgmsg.setType("changeInformation");
            NowUser.changeinfomation=false;
            new ClientConnServer().sendinfo(cgmsg);
            NowUser.cyclicBarrier.reset();
            NowUser.cyclicBarrier.await();
            if (NowUser.changeinfomation) {
                Message msg2=new Message();
                msg2.setType("getuser");
                msg2.setUser(user);
                new ClientConnServer().sendinfo(msg2);
                NowUser.cyclicBarrier.reset();
                NowUser.cyclicBarrier.await();
                //Common.setUser(user2);
                JOptionPane.showMessageDialog(null, "修改成功", "提示", JOptionPane.PLAIN_MESSAGE);
                Stage stage = (Stage) savebt.getScene().getWindow();
                stage.close();
                Personal rj = new Personal();
                try {
                    rj.start(stage);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                JOptionPane.showMessageDialog(null, "修改失败", "提示", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}
